package com.kunya.product.service.impl.specification;

import com.google.common.collect.Lists;
import com.kunya.common.common.Const;
import com.kunya.common.common.ResponseCode;
import com.kunya.common.common.ServerResponse;
import com.kunya.product.dao.specification.KindMapper;
import com.kunya.product.po.Kind;
import com.kunya.product.service.specofocation.IKindservice;
import com.kunya.product.service.specofocation.IProductSpecificationService;
import com.kunya.product.vo.KindVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zp
 * @Description:商品规格接口实现类
 * @date 2018/7/3 14:34
 */
@Service("iKindservice")
public class KindServiceImpl implements IKindservice {

    @Autowired
    private KindMapper kindMapper;

    @Autowired
    private IProductSpecificationService productSpecificationService;

    /**
     * @param productIds 商品id list
     * @author zp
     * @Description: 根据商品规格（Kind表)状态和商品id list查询商品规格volist
     * @date 2018/7/4 13:26
     */
    public ServerResponse<List<KindVo>> getListByIdsAndStatus(List<Long> productIds, String tablePrefix) {
        if (null==productIds||productIds.isEmpty() || StringUtils.isEmpty(tablePrefix)) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }
        //默认查询Kind 状态(status为1：显示)为显示的商品规格
        List<Kind> kindList = kindMapper.getListByIdsAndStatus(productIds, (byte) Const.kindStatusEnum.DISPLAY.getCode(), tablePrefix);
        if (kindList.isEmpty()) {
            return ServerResponse.createByErrorMessage("未查询到商品规格");
        }
        List<KindVo> kindVoList= Lists.newArrayList();
        for (Kind kind:kindList){
         kindVoList.add(this.assembleKindVo(kind));
        }
        return ServerResponse.createBySuccess(kindVoList);
    }

    @Override
    public ServerResponse addProductKind(Long productId, Long specificationId, String tablePrefix) {
        //TODO 根据specificationId 查询分类VO
        Kind kind=new Kind();
        kindMapper.insert(kind,tablePrefix);
        return ServerResponse.createBySuccess();
    }

    /**
     * @author zp
     * @Description: 将Kind组装为KindVo
     * @param
     * @date 2018/7/4 13:43
     */
    private KindVo assembleKindVo(Kind kind) {
        KindVo kindVo = new KindVo();
        BeanUtils.copyProperties(kind, kindVo);
        return kindVo;
    }
}
